﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EC_01
{
    public partial class EC : Form
    {

        private Boolean device_active = false;

        private byte[] ID = new byte[1] { 0x40 };
        private byte[] STOP = new byte[1] { 0x30 };
        private byte[] received = new byte[33];

        private string[] ports = new string[10] { "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10" };
        private byte u8_port_pnt = 0;
        private int u32_counter = 0;

        const byte TO_STATUS = 0x20;
        const byte FROM_STATUS =    0x19;
        const byte AT_STATUS   =    0x18;
        const byte DIRECTION_STATUS =   0x17;
        const byte IN_STATUS = 0x16;

        public EC()
        {
            InitializeComponent();
        }

        private void send_ID(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write(ID, 0, 1);
            }
            catch
            {
            }
        }

        private void change_port(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == true)
            {
                serialPort1.Close();
            }

            serialPort1.PortName = ports[u8_port_pnt++];
            if (u8_port_pnt == 10)
                u8_port_pnt = 0;

            try
            {
                serialPort1.Open();
                timer1.Enabled = true;
                timer2.Enabled = false;
            }
            catch
            {
                timer1.Enabled = false;
            }
        }

        private void check_activity(object sender, EventArgs e)
        {
            if (device_active)
            {
                device_active = false;
                pictureBox2.Image = Image.FromFile
                               (System.Environment.GetFolderPath
                               (System.Environment.SpecialFolder.MyComputer)
                               + @"d:\REPOSITORY\EC-01\Software\EC-01\EC-01\Icons\happy.png");
                label7.Text = "A lift aktív";
            }
            else
            {
                timer2.Enabled = true;
                pictureBox2.Image = Image.FromFile
                               (System.Environment.GetFolderPath
                               (System.Environment.SpecialFolder.MyComputer)
                               + @"d:\REPOSITORY\EC-01\Software\EC-01\EC-01\Icons\sad.png");
                label7.Text = "A lift nem aktív";
            }
        }

        private void serial_data_received(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                serialPort1.Read(received, 0, 33);
                
                for (int i = 0; i < 33; i++)
                {
                    if (received[i] == 0xAF - i)
                        device_active = true;
                    else
                        break;
                }

                switch(received[0])
                {
                    case TO_STATUS:
                        if (received[1] != 0x04)
                            label9.Text = Convert.ToString(received[1])+ "  ";
                        else
                            label9.Text = "x" + "  ";
                        for (int i = 2; i < 33;i++ )
                        {
                            if (received[i] != 0x04)
                                label9.Text += Convert.ToString(received[i]) + "  ";
                            else
                                label9.Text += "x" + "  ";
                                //u32_counter = i;
                                //break;
                        }
                        break;
                    case FROM_STATUS:
                        if (received[1] != 0x04)
                            label8.Text = Convert.ToString(received[1]) + "  ";
                        else
                            label8.Text = "x" + "  ";
                        for (int i = 2; i < 33; i++)
                        {
                            if (received[i] != 0x04)
                                label8.Text += Convert.ToString(received[i]) + "  ";
                            else
                                label8.Text += "x" + "  ";
                                //break;
                        }
                        break;
                    case IN_STATUS:
                        if (received[1] != 0x00)
                            label13.Text = "O" + "  ";
                        else
                            label13.Text = "x" + "  ";
                        for (int i = 2; i < 33; i++)
                        {
                            if (received[i] == 0x01)
                                label13.Text += "O" + "  ";
                            else
                                label13.Text += "x" + "  ";
                        }
                        break;
                    case AT_STATUS:
                        label10.Text = Convert.ToString(received[1]);
                        break;
                    case DIRECTION_STATUS:
                        if (received[1] == 0)
                            label11.Text = "Fel";
                        else if (received[1] == 1)
                            label11.Text = "Le";
                        else
                            label11.Text = "Áll";
                        break;
                }
            }
            catch { }
        }

        private void form_close(object sender, FormClosingEventArgs e)
        {
            try
            {
                serialPort1.Close();
            }
            catch
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write(STOP, 0, 1);
                label8.Text = "";
                label9.Text = "";
                label10.Text = "";
                label13.Text = "";
            }
            catch
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label8.Text = "";
            label9.Text = "";
            label10.Text = "";
            label13.Text = "";
        }
    }
}
